package com.basic.library.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.basic.library.base.BaseApplication;

import java.util.ArrayList;

public abstract class CommonAdapter<T> extends BaseAdapter {
	protected LayoutInflater mInflater;
	protected Context mContext;
	public ArrayList<T> mDatas;
	protected final int mItemLayoutId;

	public CommonAdapter(Context context, ArrayList<T> mDatas, int itemLayoutId) {
		if(context != null){
			this.mContext = context;
		}else{
			this.mContext = BaseApplication.getInstance().getApplicationContext();
		}
		this.mInflater = LayoutInflater.from(mContext);
		this.mDatas = mDatas;
		this.mItemLayoutId = itemLayoutId;
	}

	public void onDateChange(ArrayList<T> result) {
		this.mDatas = result;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public T getItem(int position) {
		T t = null;
		try {
			t = mDatas.get(position);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}

	@Override
	public long getItemId(int position){
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		if(this.mContext)
		final ViewHolder viewHolder = getViewHolder(position, convertView,
				parent);
		try {
			convert(viewHolder, getItem(position));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return viewHolder.getConvertView();
	}

	public abstract void convert(ViewHolder helper, T item);

	private ViewHolder getViewHolder(int position, View convertView,
			ViewGroup parent) {
		return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
				position);
	}

}
